package com.qdxwx.manage.controller.my;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.coupon.CouponCodeService;
import com.qdxwx.data.service.order.OrderDetailService;
import com.qdxwx.data.service.order.OrderService;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.coupon.CouponCode;
import com.qdxwx.models.enums.OrderType;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.order.Order;
import com.qdxwx.models.order.OrderDetail;
import com.qdxwx.models.order.OrderQuery;
import com.qdxwx.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 课程订单Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/my/order")
public class MyOrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CouponCodeService couponCodeService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;

    @GetMapping("/list.html")
    public String list() {
        return "my/my-order-list";
    }

    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        if (manager == null) {
            return TableDataInfo.error("未登录！");
        }
        OrderQuery order = new OrderQuery();
        order.setOrderNo(request.getParameter("orderNo"));
        order.setOrderType(OrderType.COURSE.toString());
        order.setStatus(request.getParameter("status"));
        order.setUserId(manager.getUserId());
        List<Order> list = orderService.getListByTeacher(order, PageRequest.form(request));
        long count = orderService.countByTeacher(order);
        return TableDataInfo.success(list, count);
    }

    /**
     * 订单详情页面
     */
    @RequestMapping("/view.html")
    public String companyOrderInfo(HttpServletRequest request, ModelMap mmap) {
        String orderNo = request.getParameter("orderNo");
        if(TKString.isEmpty(orderNo)) return error404;
        Order order = orderService.getByOrderNo(orderNo);
        if(order == null) return error404;
        User user = userService.get(order.getUserId());
        if(user == null) return error500;
        CouponCode couponCodeDTO = couponCodeService.get(order.getCouponCodeId());
        if (couponCodeDTO == null) {
            couponCodeDTO = new CouponCode();
        }
        List<OrderDetail> orderDetails = orderDetailService.getListByOrderId(order.getOrderId());
        mmap.put("user", user);
        mmap.put("order", order);
        mmap.put("couponCodeDTO", couponCodeDTO);
        mmap.put("orderDetails", orderDetails);
        return "my/my-order-info";
    }
}
